package cc.linker.steplink.crm.merchant.controller.acs;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.RightService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/right")
public class RightController {
    @Autowired
    private RightService rightService;
    @Autowired
    private UserInfoUtil userInfoUtil;
    /**
     * 权益列表
     *
     * @param ：pageIndex 页码
     * @param ： pageSize  页面大小
     */
    @GetMapping("/list")
    public Object getRightList(@RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return rightService.getRightList(params);
    }

    /**
     * 全部权益列表
     *
     * @param ：ids 权益ids
     */
    @GetMapping("/alllist")
    public Object getAllRightList(String ids, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        params.put("ids", ids);
        return rightService.getAllRightList(params);
    }

    /**
     * 权益详情
     *
     * @param ：id 主键
     */
    @GetMapping("/info")
    public Object getRightInfo(Long id) {
        if(id==null){
            throw GlobalException.build("参数不能为空！");
        }
        return rightService.getRightInfo(id);
    }

    /**
     * 保存权益
     *
     * @param ：id 主键
     * @param ：rightName 权益名称
     * @param ：showName 展示名称
     * @param ：rightCover 权益图片
     * @param ：rightDesc 简介
     *
     */
    @PostMapping("/save")
    public void save(@RequestBody Map<String, Object> param,HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        param.put("orgId",userinfo.getOrgId());
        rightService.save(param);
    }

    /**
     * 删除权益
     *
     * @param ：id 主键
     *
     */
    @PostMapping("/delete")
    public void delete(@RequestBody Map<String, Object> param){
        if(StringUtils.isEmpty(param.get("id"))){
            throw GlobalException.build("参数不能为空！");
        }
        Long id = Long.valueOf(param.get("id").toString());
        rightService.delete(id);
    }
}
